package ca.compsci.opent.doctool;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * The OpenTDoc tool's entry point.
 * 
 * @since 0.0.2
 */
public final class OpenTDoc
{
	/**
     * Name of tool. That is, the command to run the tool from command-line.
	 * 
	 * @since 0.0.1a
     */
    private static final String TOOL_NAME = "opentdoc";

    /**
	 * Do not allow construction
	 * 
	 * @since 0.0.1a
	 */
    private OpenTDoc()
    {
    }

    /**
     * Program entry point.
     *
     * @param args arguments to pass the tool
	 * 
	 * @since 0.0.1a
     */
    public static void main(String[] args)
    {
        // Check if args supplied are valid
        if (!properArgs(args))
        {
            System.err.println("Improper usage. Usage:");
            System.err.println(usage());
            System.err.println("Run with argument \'help\' for details.");
            System.exit(1);
        }

        // 'help' argument prints help
        if (args[0].equalsIgnoreCase("help"))
        {
            System.out.println(help());
            return;
        }

        // Read file
        try
        {
            List<String> lines = readFromFile(args[0]);
			
			for (String l : lines)
				System.out.println(l);
			
			SourceCodeParser parser = new SourceCodeParser(lines);
			parser.parse();
			
			System.out.println("vars:");
			for (Var v : parser.getVarList())
			{
				System.out.println(v);
			}
        }
        catch (IOException e)
        {
            System.err.println(e.getMessage());
			System.exit(1);
        }
		catch (OpenTDocException e)
		{
			System.err.println(e.getMessage());
			System.exit(1);
		}		
    }
	
	/**
	 * Parses the source code text provided.
	 * 
	 * @param src 
	 * @throws OpenTDocException 
	 * 
	 * @since 0.0.1a
	 */
	private static void parseSourceCode(List<String> src) throws OpenTDocException
	{
		//TODO: Should parser be a separate class? I think so.
		throw new OpenTDocException("Parse not yet supported");
	}

    /**
     * Reads text from a file.
     *
     * @param filename file to read next from
     *
     * @return a {@link String} containing the text of the file
     *
     * @throws IOException when an I/O error occurs
	 * 
	 * @since 0.0.2
     */
    private static List<String> readFromFile(String filename) throws IOException
    {
        File file = new File(filename);
		
        if (!file.exists())
        {
            throw new FileNotFoundException(String.format("%s does not exist", file));
        }

		List<String> lines = new ArrayList<String>();
		
        BufferedReader in = new BufferedReader(new FileReader(filename));

        try
        {
			String line;
			while ((line = in.readLine()) != null)
			{
				lines.add(line);
			}
        }
        finally
        {
            in.close();
        }
		
		return lines;
    }

    /**
     * Checks if args supplied are valid.
     *
     * @param args arguments supplied to the tool
     *
     * @return {@code true} if {@code args} contains valid arguments in valid
     *	order, {@code false} otherwise
	 * 
	 * @since 0.0.1a
     */
    private static boolean properArgs(String[] args)
    {
        if (args.length != 1)
        {
            return false;
        }

        return true;
    }

    /**
     * Returns the usage text of the tool
     *
     * @return usage text
	 * 
	 * @since 0.0.1a
     */
    private static String usage()
    {
        return TOOL_NAME + " sourcefile";
    }

    /**
     * Returns help text for the tool.
     *
     * @return help text
	 * 
	 * @since 0.0.1a
     */
    private static String help()
    {
        return usage() + "\n\n" + "No help yet -- beta";
    }
}
